<?php 
  
/*

	Project -> Blogmer - Open Source Blogging Software
	Author  -> Yashpal
	Type    -> PHP Controller
	Include -> User related functions.
	
	Some part is taken from
			http://codeigniter.com/forums/viewthread/60584/P0/
			Author - Michael Wales 
			Site   - MichaelWales.com


	There are four functions.Two are register and login and other two are dependent functions.
	register -> check for already session if not found redirect to Registration form.
				It uses Validation,Table,Date 	helpers & libraries.
				In function checks for already username & Email.
				It uses _check_username function for checking already existing emails and usernames.
				If all ok it register/save user information.
				
	login    -> check for already session if not found redirect to login form.
			    It uses Validation,Table,Date,string 	helpers & libraries.
			    In function checks for username & password in database and save sessions.
			    It uses _check_login function for checking username and password exist in database.
			    If all ok it login the user and save session data for current login.

*/ 
class Users extends Controller {

	function Users()
	{
		parent::Controller();
		$this->Settings_model->BlogSettings();
		
	}
	
	function index()
	{
		 $login = $this->session->userdata('logged_in');
		if($login)
		{		 redirect('blog/','refresh'); }
		else
		{		 redirect('users/Register','refresh'); }
		
	}
	function Register()
	{
		
		$data['title']="User Registration";
		$data['heading']="User Registration";
				
		 $login = $this->session->userdata('logged_in');
		if($login)
		{		 redirect('blog/','refresh'); }
		
		$this->load->library('validation');
		$this->load->library('table'); 
		$this->load->helper('security');
		$this->load->helper('date');
				
		$rules['username'] = "trim|required|min_length[4]|max_length[20]|callback__check_username";
		$rules['email'] = "trim|required|valid_email|max_length[100]";
		$rules['password'] = "trim|required|min_length[6]|max_length[20]";
		$rules['confirmpassword']="trim|matches[password]";
		$rules['firstname'] = "required";
		$rules['murl'] = "required";
		$rules['lastname'] = "required";
		
		$this->validation->set_rules($rules);

		$fields['username'] = 'Username';
        $fields['email'] = 'Email ID';
		$fields['murl'] = 'URL - Website link';
        $fields['firstname'] = 'First Name';
		$fields['lastname'] = 'Last Name';
        $fields['password'] = 'Password';
        $fields['confirmpassword'] = 'Confirm Password';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{	$this->Users_model->UserRegistration();   	}
		else 
		{
          		//load it first till user fill all details correctly.
		  		$data=final_sidebar_data();
				$data['title']="Register | ".$this->session->userdata('BLOGName');
				if($this->session->userdata('EnableRegistration')==1)  
					$data['content']=$this->load->view('users/registration',$data,TRUE);
				else
					$data['content']="&nbsp;Registrations are closed at this time";	
				$data['where']='single';
				$this->load->view('main',$data);
				
        }
			
	}
	
	
	
	//Logout
	function Logout()
	{
			$this->session->sess_destroy();
			$info['title']="Information Page";
			$info['header']="Information";
			$info['message']="You have successfully logged out.";
			$info['url']="/blog/";
			$info['time']="2";
			
		  	$this->load->view('info',$info);
		
	}
	
	function reload_settings()
	{ $this->Settings_model->reload_settings(); }
	

	// Check username/email for uniqueness
    function _check_username($username) 
	{
        $email = $this->validation->email;
        $query = $this->db->getwhere('users', array('MName'=>$username), 1, 0);
        if ($query->num_rows() != 0) 
		{
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        } 
		elseif($email!="") 
		{
            $query = $this->db->getwhere('users', array('MEmail'=>$email), 1, 0);
            if ($query->num_rows() != 0) 
			{
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }
    
   
	
	//Activate user based on Activation Code
	function ActivateUser($id,$act_code)
	{
		$this->Users_model->ActivateUser();
	}
	

	//Get Email from user and generate a reset url
	function ForgetPassword()
	{
		$data['title']="Forget Password";
		$this->load->library('validation');
		$this->load->helper('security');
		$this->load->library('table');
		
				
		$rules['username'] = "trim|min_length[4]|max_length[20]";
		$rules['email'] = "trim|required|valid_email|max_length[100]";
		$this->validation->set_rules($rules);

		$fields['username'] = 'Username';
        $fields['email'] = 'Email ID';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
				//all details are ok.Now save these. 
				//$this->load->model('Users_model','',TRUE);
				$this->Users_model->ForgetPassword();
				
		}
		else 
		{
          		//load it first till user fill all details correctly.
		  		
				$data=final_sidebar_data();
				$data['title']="Forget Password | ".$this->session->userdata('BLOGName');
				$data['content']=$this->load->view('users/forget_password',$data,TRUE);
				$data['where']='single';
				$this->load->view('main',$data); 
				   
        }
	}
	
	//Reset Password based on Activation Code
	function ResetPassword()
	{
		$this->Users_model->ResetPassword();
	}
	
	function ViewProfile($ID)
	{ 
		$data=final_sidebar_data();
		$data['title']="View Profile";
		$data['content']=$this->Users_model->ViewProfile($ID);
		$data['where']='single';
		$this->load->view('main',$data); 
	
	}
	
}

